package au.org.nh.pdf.view;

import java.io.IOException;
import java.io.StringReader;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.servlet.ServletContextURIResolver;

import au.org.nh.dao.PatientInfo;
import au.org.nh.dao.PatientInfoDAO;
import au.org.nh.dao.hsqldb.impl.HSQLDBPatientInfoDAOImpl;
import au.org.nh.dao.mongo.impl.DBException;
import au.org.nh.pdfgen.FOPPDFGeneratorImpl;
import au.org.nh.pdfgen.PDFGenerator;
import au.org.nh.util.file.ResourceFileReader;
import au.org.nh.xml.generator.XMLGenerator;
import au.org.nh.xml.generator.XMLGeneratorUsingReflection;

public class PDFViewer extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected URIResolver uriResolver;

	@Override
	public void init() throws ServletException {
		this.uriResolver = new ServletContextURIResolver(getServletContext());
	}

	@Override
	public final void doGet(final HttpServletRequest request,
			final HttpServletResponse response) throws IOException {
		byte[] content = null;

		final String patientId = request.getParameter("id");
		final PatientInfoDAO patientInfoDAO = new HSQLDBPatientInfoDAOImpl();
		PatientInfo patientInfo = null;
		try {
			patientInfo = patientInfoDAO.getById(Long.parseLong(patientId));
		} catch (final NumberFormatException e) {
			e.printStackTrace();
		} catch (final DBException e) {
			e.printStackTrace();
		}

		final XMLGenerator xmlgen = new XMLGeneratorUsingReflection();
		String xmlPatientInfo = null;
		try {
			xmlPatientInfo = xmlgen.generate(patientInfo);
		} catch (final IllegalArgumentException e) {
			e.printStackTrace();
		} catch (final IllegalAccessException e) {
			e.printStackTrace();
		}

		// Printing xml generated
		System.out.println("XML generated " + xmlPatientInfo);
		final PDFGenerator pdfgen = new FOPPDFGeneratorImpl();
		final StreamSource xmlsource = new StreamSource(new StringReader(
				xmlPatientInfo));
		/* Potential for injection - avoiding static calls */
		final ResourceFileReader resourceFileReader = new ResourceFileReader();
		final StreamSource xslsource = new StreamSource(
				resourceFileReader.getFile("nh.xsl"));

		content = pdfgen.generate(xmlsource, xslsource, uriResolver);

		if (content != null && content.length > 0) {
			// Internet Explorer doesn't like caching over SSL
			response.setHeader("Cache-Control", "");
			response.setHeader("Pragma", "");
			// response.setHeader("Content-Disposition",
			// "attachment; filename=NHPatientInfo.pdf");
			response.setHeader("Content-Disposition:",
					"inline;filename=" + patientInfo.getFirstName() + "_"
							+ patientInfo.getLastName()
							+ "_Cancer Support Form.pdf");

			response.setContentType("application/pdf");
			response.setContentLength(content.length);

			response.getOutputStream().write(content);
			response.getOutputStream().flush();

		}
	}

}
